package com.zrc.superframe_aop.aspect

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.PermissionUtils
import com.zrc.superframe_aop.annotation.Permission
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Descrip: 申请系统权限注解
 */
@Aspect
class PermissionAspectJ : FatherAspect() {

    //方法切入点
    @Pointcut("execution(@com.zrc.superframe_aop.annotation.Permission * *(..))")
    fun permissioPointcut() {
    }

    @Around("permissioPointcut() && @annotation(permission)")
    @Throws(Throwable::class)
    fun aroundJoinPoint(joinPoint: ProceedingJoinPoint, permission: Permission) {
        PermissionUtils.permission(*permission.value)
            .callback(object : PermissionUtils.FullCallback {
                override fun onGranted(permissionsGranted: List<String>) {
                    joinPoint.proceed() //获得权限，执行原方法
                }

                override fun onDenied(permissionsDeniedForever: List<String>, permissionsDenied: List<String>) {
                    logger("权限申请被拒绝:${GsonUtils.toJson(permissionsDenied)}")
                    //if (XAOP.getOnPermissionDeniedListener() != null) {
                    //    XAOP.getOnPermissionDeniedListener().onDenied(permissionsDenied);
                    //}
                }
            })
            .request()
    }
}